﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PERMISSION.Base;

namespace ERP_RORZE_PRODUCT.Product.MO
{
    public partial class frm_MOOptionAdd : DevExpress.XtraEditors.XtraForm
    {
        ClsMODAO clsMODAO = new ClsMODAO();
        DataTable dt;
        ClsMOOptionTable b = new ClsMOOptionTable();
        private LogUtils log;

        public frm_MOOptionAdd()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            b.ClampTime = float.Parse(txtClampTime.Text);
            b.Deadline = dateDeadline.DateTime;
            b.Finishdate = dateFinishDate.DateTime;
            b.Finished = checkFinished.Checked;
            b.FinishMachine = "";
            b.JigID = txtJigType.Text;
            b.LastOption = checkLastOption.Checked;
            b.MachineID = cbMachineID.Text;
            b.MONo = cbMONo.Text;
            b.MOQty = int.Parse(txtMOQty.Text);
            b.Note = txtNote.Text;
            b.OptionID = cbOptionID.Text;
            b.PartID = cbPartID.Text;
            b.ProTime = float.Parse(txtProTime.Text);
            b.Shift = cbShift.Text;
            clsMODAO.InsertMOOption(b);

            //write Log
            log.Table = LogConstant.Table.Product.MOOption;
            log.Task = LogConstant.Task.Insert;
            log.Description = "MONo: " + b.MONo + "; PartID: " + b.PartID + "; OptionID: " + b.OptionID + "; Deadline: " + b.Deadline;
            log.WriteLog();

            MessageBox.Show("Đã nhập xong");

            cbMONo.Text = "";
            cbMachineID.Text = "";
            cbOptionID.Text = "";
            cbPartID.Text = "";
            cbShift.Text = "";
            txtClampTime.Text = "0";
            txtJigType.Text = "";
            txtMOQty.Text = "0";
            txtNote.Text = "";
            txtProTime.Text = "0";
            checkFinished.Checked = false;
            checkLastOption.Checked = false;
            cbMONo.Focus();

        }

        private void frm_MOOptionAdd_Load(object sender, EventArgs e)
        {
            //danh sách MONo
            DataTable dtMONo = new DataTable();
            dtMONo = clsMODAO.GetAllMO();
            DataRow rMONo = dtMONo.NewRow();
            rMONo["MONo"] = "";
            dtMONo.Rows.InsertAt(rMONo, 0);
            cbMONo.DataSource = dtMONo;
            cbMONo.DisplayMember = "MONo";
            cbMONo.ValueMember = "MONo";
            //List<ClsMOOptionTable> objs = new List<ClsMOOptionTable>();
            //ClsMOOptionTable obj1 = new ClsMOOptionTable();
            //obj1.MONo = "";
            //obj1.PartID = "";
            //objs.Add(obj1);
            //foreach (DataRow r in dtMONo.Rows)
            //{
            //    ClsMOOptionTable obj = new ClsMOOptionTable();
            //    obj.MONo = r["MONo"].ToString();
            //    obj.PartID = r["PartID"].ToString();
            //    objs.Add(obj);
            //}
            //cbMONo.DataSource = objs;
            //cbMONo.DisplayMember = "MONo";
            //cbMONo.ValueMember = "MONo";

            //danh sách PartID
            DataTable dtPartID = new DataTable();
            dtPartID = clsMODAO.GetAllPartID();
            DataRow rPartID = dtPartID.NewRow();
            rPartID["PartNo"] = "";
            //rPartID["PartID"] = "";
            dtPartID.Rows.InsertAt(rPartID, 0);
            cbPartID.DataSource = dtPartID;
            cbPartID.DisplayMember = "PartNo";
            cbPartID.ValueMember = "PartNo";
            

            //danh sách OptionID
            DataTable dtOptionID = new DataTable();
            dtOptionID = clsMODAO.GetAllOptionID();
            DataRow rOptionID = dtOptionID.NewRow();
            rOptionID["OptionID"] = "";
            //rPartID["PartID"] = "";
            dtOptionID.Rows.InsertAt(rOptionID, 0);
            cbOptionID.DataSource = dtOptionID;
            cbOptionID.DisplayMember = "OptionID";
            cbOptionID.ValueMember = "OptionID";

            //danh sách MachineID
            DataTable dtMachineID = new DataTable();
            dtMachineID = clsMODAO.GetAllMachineID();
            DataRow rMachineID = dtMachineID.NewRow();
            rMachineID["MachineID"] = "";
            //rPartID["PartID"] = "";
            dtMachineID.Rows.InsertAt(rMachineID, 0);
            cbMachineID.DataSource = dtMachineID;
            cbMachineID.DisplayMember = "MachineID";
            cbMachineID.ValueMember = "MachineID";
            
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}